import 'core-js'
import express from 'express'
import path from 'path'
import { ENV } from '@server/env'
import morgan from 'morgan'
import fileUpload from 'express-fileupload'
import { errorHandler } from './error/error-handler'
import { api } from './api/index'
import { initializeDataStore } from './lib/data-store'

const app = express()
app.use(express.static(path.resolve('./www')))
app.use(express.json())
app.use(morgan('dev'))
app.use(fileUpload())

// 初始化数据存储
initializeDataStore()
  .then(() => {
    console.log('Data store ready')
  })
  .catch((error) => {
    console.error('Failed to initialize data store:', error)
    process.exit(1)
  })

api(app)
app.use(errorHandler)

// 启动服务器
app.listen(ENV.PORT, () => {
  console.log(`server is running on port ${ENV.PORT}`)
})
